(function() {
  var load_class, login_fail, login_success, try_login;

  window.login = {};

  try_login = function() {
    if ($('#username-box').val() === '' || $('#password-box').val() === '') return;
    return $.post('/users/validate', {
      u: $('#username-box').val(),
      p: sha256_digest($('#password-box').val())
    }, function(data) {
      if (data.error != null) {
        return login_fail();
      } else {
        return login_success(data);
      }
    });
  };

  login_success = function(data) {
    $('#user-display').text(data.rn);
    if (!data.uc) {
      0;
    } else if (typeof data.uc === 'array') {
      0;
    } else {
      load_class(uc);
    }
    $('#login-box').modal('hide');
    $('.navbar').fadeIn();
    return $('#dojo').fadeIn();
  };

  login_fail = function() {
    console.log("Failing");
    return $.meow({
      message: "Username or password not valid!"
    });
  };

  load_class = function(uc) {
    $('.lang-img').hide();
    return $("#" + data.uc.la + "-img").show();
  };

  login.initialize = function() {
    $('#login-box').modal({
      backdrop: false,
      keyboard: false
    });
    return $('#login-btn').click(try_login);
  };

}).call(this);

(function() {

  $(function() {
    login.initialize();
    $('.btn-group').button();
    return $('.dropdown-toggle').dropdown();
  });

}).call(this);
